#we import os.path so we can check the existance of our save of contest

import os.path

#asignment of varriables needed to run programe

tab = [ [" "," "," "],[" "," "," "],[" "," "," "] ]     #two dimensional table wich we will use to create a board
player = "X"                                            #the starting player 
contest = 0
Dict_Width = {'A':0,'a':0,'B':1,'b':1,'C':2,'c':2}      #the dictionary used latter for conservation good and elimination bad replies

#asignment of constants needed to run programe

No_Contest = 0
X_Wins = 1
O_wins = 2
Draw = 3
path = "/tmp/contest.txt"                               #this path we will use for saving contest into a file

#definition of function wich will project the board on screen

def map_screen():

#format characters are put between a numerous characters making "screen"

    screen = '''
    1   2   3
  -------------
A | %s | %s | %s |
  -------------
B | %s | %s | %s |
  -------------
C | %s | %s | %s |
  -------------'''

#this function will return the "screen" and format characters are replaced by elements of table making our board

    return screen % (tab[0][0], tab[0][1], tab[0][2], tab[1][0], tab[1][1], tab[1][2], tab[2][0], tab[2][1], tab[2][2])

#this function will upload coordinates from player and use them to fill in table filling our board

def GET_and_SET_value(P):

    while True:                                         #only if both replies are correct the loop will end
        print "%s plays" % P

        reply_h = raw_input("enter height:")            #acquirement of first coordinate
        reply_w = raw_input("enter width:")             #acquirement of second coordinate
    
        if not reply_h.isdigit():
            print "entered inproper coordinates, please use numbers 0, 1 or 2"
    
        elif not reply_w in Dict_Width:
            print "entered inproper coordinates, please use letters A, B or C"

        else:
        
            reply_h = int(reply_h) - 1                  #becouse we use numbers from 1-3 not 0-2 we need to lover reply by one and convert it into integer
            reply_w = Dict_Width[reply_w]               #using our dictionary we convert letter into a value

            if not (reply_h >= 0 and reply_h <= 2):
                print "please use numbers from 1 to 3"

            elif tab[reply_w][reply_h] !=" ":
                print "this field is already filled in"

            else: 
                tab[reply_w][reply_h] = P               #filling in table with current players sign using got coordinates
                break                                   #ending the loop when coordinates are correct


def contest_check(P):

    contest = 0

    # if fields 1A, 1B, and 1C are filled in with X the contest is X_wins
    if (tab[0][0] == "X" and tab[0][1] == "X" and tab[0][2] == "X"):
        contest = X_Wins

    # if fields 2A, 2B, and 2C are filled in with X the contest is X_wins
    if (tab[1][0] == "X" and tab[1][1] == "X" and tab[1][2] == "X"):
        contest = X_Wins

    # if fields 3A, 3B, and 3C are filled in with X the contest is X_wins
    if (tab[2][0] == "X" and tab[2][1] == "X" and tab[2][2] == "X"):
        contest = X_Wins

    # if fields 1A, 2A, and 3A are filled in with X the contest is X_wins
    if (tab[0][0] == "X" and tab[1][0] == "X" and tab[2][0] == "X"):
        contest = X_Wins

    # if fields 1B, 2B, and 3B are filled in with X the contest is X_wins
    if (tab[0][1] == "X" and tab[1][1] == "X" and tab[2][1] == "X"):
        contest = X_Wins

    # if fields 1C, 2C, and 3C are filled in with X the contest is X_wins
    if (tab[0][2] == "X" and tab[1][2] == "X" and tab[2][2] == "X"):
        contest = X_Wins

    # if fields 3A, 2B, and 1C are filled in with X the contest is X_wins
    if (tab[2][0] == "X" and tab[1][1] == "X" and tab[0][2] == "X"):
        contest = X_Wins

    # if fields 1A, 2B, and 3C are filled in with X the contest is X_wins
    if (tab[0][0] == "X" and tab[1][1] == "X" and tab[2][2] == "X"):
        contest = X_Wins
    
    # if fields 1A, 1B, and 1C are filled in with O the contest is O_wins 
    if (tab[0][0] == "O" and tab[0][1] == "O" and tab[0][2] == "O"):
        contest = O_wins

    # if fields 2A, 2B, and 2C are filled in with O the contest is O_wins
    if (tab[1][0] == "O" and tab[1][1] == "O" and tab[1][2] == "O"):
        contest = O_wins

    # if fields 3A, 3B, and 3C are filled in with O the contest is O_wins
    if (tab[2][0] == "O" and tab[2][1] == "O" and tab[2][2] == "O"):
        contest = O_wins

    # if fields 1A, 2A, and 3A are filled in with O the contest is O_wins
    if (tab[0][0] == "O" and tab[1][0] == "O" and tab[2][0] == "O"):
        contest = O_wins

    # if fields 1B, 2B, and 3B are filled in with O the contest is O_wins
    if (tab[0][1] == "O" and tab[1][1] == "O" and tab[2][1] == "O"):
        contest = O_wins

    # if fields 1C, 2C, and 3C are filled in with O the contest is O_wins
    if (tab[0][2] == "O" and tab[1][2] == "O" and tab[2][2] == "O"):
        contest = O_wins

    # if fields 3A, 2B, and 1C are filled in with O the contest is O_wins
    if (tab[2][0] == "O" and tab[1][1] == "O" and tab[0][2] == "O"):
        contest = O_wins

    # if fields 1A, 2B, and 3C are filled in with O the contest is O_wins
    if (tab[0][0] == "O" and tab[1][1] == "O" and tab[2][2] == "O"):
        contest = O_wins

    # if none of fields are empty contest is Draw
    if (tab[0][0] != " " and tab[0][1] != " " and tab[0][2] != " " and tab[1][0] != " " and tab[1][1] != " " and tab[1][2] != " " and tab[2][0] != " " and tab[2][1] != " " and tab[2][2] != " "): 
        contest = Draw

    #the function will return the contest

    return contest

#if our save don't exists we create a new one with 0,0,0

if not os.path.exists(path):
    open(path, "a").write("0,0,0")

#we open a file in "read" mode and read from it current contest

File = open(path, "r")
save = File.read()

#conversion of save into a table

save = save.split(",")

#annoucement of current results

print "Wins_X: %s  Wins_O: %s Draws: %s" % ( save[0], save[1], save[2] )

#the game will continue until contest will be "No_Contest"

while True:

    print map_screen()                             #our board projecting function

    GET_and_SET_value(player)                      #our funtion wich will upload coordinates and use them to fill in a field of board

    if contest_check(player) != No_Contest:        #if contest from function contest_check will not equal No_contest the game will end
        print map_screen()
        
        if contest_check(player) == X_Wins:        #if contest equals X_wins projects Player X Wins and change the first element in save into old plus one
            print "Player X Wins!"
            save[0] = str( int(save[0])+ 1 )

        if contest_check(player) == O_wins:        #if contest equals O_wins projects Player O Wins and change the second element in save into old plus one
            print "Plauer O Wins!"
            save[1] = str( int(save[1])+ 1 )

        if contest_check(player) == Draw:          #if contest equals Draw projects Draw and change the third element in save into old plus one
            print "Draw"
            save[2] = str( int(save[2])+ 1 )

        break

    #conditional used for changing current player

    if player == "X": 
        player = "O"
    else:
        player = "X"


#annoucement of new results

print "Wins_X: %s  Wins_O: %s Draws: %s" % ( save[0], save[1], save[2] )

#conversation of save again into a string and closing file

save = ",".join(save)
File.close()

#reopening a file in write mode and overwriting the save file

File_new = open(path, "w")
File_new.write(save)

#closing new file and last line of XOXO.py

File_new.close